package com.okilo.online.app.common

import android.content.Context
import android.view.Gravity
import android.view.WindowManager
import com.okilo.online.app.BuildConfig
import com.okilo.online.app.OkiloApplication
import com.okilo.online.app.R
import com.okilo.online.app.base.OkiloBaseDlog
import com.okilo.online.app.request.HttpHelper
import com.okilo.online.app.request.IHttpCallback
import com.okilo.online.app.tools.OkiloDensityUtil
import com.okilo.online.app.tools.extensions.getColorRes
import com.okilo.online.app.tools.extensions.getStringRes
import org.json.JSONObject

/**
 * CommonDialog
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:32
 * @Annotation：
 */
abstract class OkiloComDlog(context: Context): OkiloBaseDlog(context) {

    override fun initializeOkiloWindow() {
        val params = window?.attributes
        params?.width = OkiloApplication.app.getPhoneWidth() - OkiloDensityUtil.dp2px(32f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    /**
     * post
     */
    fun doPostAction(httpUrl: String, params: JSONObject, callback: IHttpCallback){
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                callback.onRequestSuccess(data)
            }

            override fun onRequestError(error: JSONObject) {
                callback.onRequestError(error)
                showOkiloToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                callback.onRequestFinish()
            }
        })
    }

    /**
     * 添加埋点
     */
    fun addEventPointRequestAction(eventName: String, eventRemark: String = "") {
        HttpHelper.addPoint(eventName,eventRemark)
    }

    /**
     * getString
     */
    fun getString(strId: Int): String{
        return context.getStringRes(strId)
    }

    /**
     * getColor
     */
    fun getColor(color: Int): Int{
        return context.getColorRes(color)
    }

    override fun show() {
        super.show()
        HttpHelper.setTag(this)
    }


    override fun onStart() {
        super.onStart()
        trackOkiloDialogShow()
    }
    override fun dismiss() {
        super.dismiss()
        trackOkiloDialogDismiss()
        HttpHelper.cancelTag(this)
    }

    /**
     * 启动埋点
     */
    private fun trackOkiloDialogShow() {
        if (enableDialogTrack){
            addEventPointRequestAction("${BuildConfig.appName}_Dialog_${dialogName}_show".uppercase(),dialogRemark)
        }
    }

    /**
     * 关闭埋点
     */
    private fun trackOkiloDialogDismiss() {
        if (enableDialogTrack){
            addEventPointRequestAction("${BuildConfig.appName}_Dialog_${dialogName}_dismiss".uppercase(),dialogRemark)
        }
    }
}